package com.jiutong.meipenggang.ui.fragments;

import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.gyf.barlibrary.ImmersionBar;
import com.gyf.barlibrary.OnKeyboardListener;
import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.BaseFragment;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.MineFramentInfoBean;
import com.jiutong.meipenggang.http.RetrofitHandle;
import com.jiutong.meipenggang.http.model.interfaces.BaseBiz;
import com.jiutong.meipenggang.http.present.MineFramentInfoPresent;
import com.jiutong.meipenggang.ui.activitys.BankCardActivity;
import com.jiutong.meipenggang.ui.activitys.BindingBankCardActivity;
import com.jiutong.meipenggang.ui.activitys.HelpCenterActivity;
import com.jiutong.meipenggang.ui.activitys.HuoDongActivity;
import com.jiutong.meipenggang.ui.activitys.InviteFriendActivity;
import com.jiutong.meipenggang.ui.activitys.MeActivity;
import com.jiutong.meipenggang.ui.activitys.MsgCenterActivity;
import com.jiutong.meipenggang.ui.activitys.SettingActivity;
import com.jiutong.meipenggang.ui.activitys.TotalMoneyActivity;
import com.jiutong.meipenggang.ui.activitys.TradeRecordActivity;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.GlideCircleTransform;
import com.jiutong.meipenggang.views.RoundImageView;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author Veken
 * @date 2017/9/20
 */

public class MineFragment extends BaseFragment implements BaseBiz {

    @Bind(R.id.rl_setting)
    RelativeLayout rlSetting;
    @Bind(R.id.ll_mine_fragment)
    LinearLayout llMineFragment;
    @Bind(R.id.tv_minefragment_login)
    TextView tvMinefragmentLogin;
    @Bind(R.id.iv_headpic)
    RoundImageView ivHeadPic;
    @Bind(R.id.rl_zichan)
    RelativeLayout rlZichan;
    @Bind(R.id.rl_my_zichan)
    RelativeLayout rlMyZichan;
    @Bind(R.id.rl_trade_record)
    RelativeLayout rlTradeRecord;
    @Bind(R.id.tv_mine_bank)
    TextView tvMineBank;
    @Bind(R.id.rl_bank)
    LinearLayout rlBank;
    @Bind(R.id.rl_act)
    RelativeLayout rlAct;
    @Bind(R.id.rl_msg)
    RelativeLayout rlMsg;
    @Bind(R.id.rl_help_center)
    RelativeLayout rlHelpCenter;
    @Bind(R.id.rl_invite_friend)
    RelativeLayout rlInviteFriend;

    private BaseDialog dialog;
    private String imgPath;
    private String TAG = getClass().getSimpleName();

    private MineFramentInfoPresent mineFramentInfoPresent;
    private boolean isBindingBankCard;
    private boolean mHidden;

    @Override
    protected int getLayoutResouceId() {
        return R.layout.fragment_person;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!SpConfig.getInstance().getBool(Constants.IS_LOGIN)){
            tvMinefragmentLogin.setText("登录/注册");
        }
        if (mHidden)
            return;
        ImmersionBar.with(this).fitsSystemWindows(false).
                //单独指定软键盘模式
                        keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
                .setOnKeyboardListener(new OnKeyboardListener() {    //软键盘监听回调
                    @Override
                    public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
//                        LogUtils.e(isPopup);  //isPopup为true，软键盘弹出，为false，软键盘关闭
                    }
                }).statusBarDarkFont(false).init();
        //只会执行一次？？？
        doNetWork();
        //沉浸式导致的软键盘关闭不及时，界面上调
    }

    //点击事件
    @OnClick({R.id.rl_setting, R.id.tv_minefragment_login, R.id.iv_headpic, R.id.rl_zichan, R.id.rl_trade_record, R.id.rl_bank, R.id.rl_act, R.id.rl_msg, R.id.rl_help_center, R.id.rl_invite_friend})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_minefragment_login:
                if (isLogin()) {

                }
                break;
            case R.id.iv_headpic:
                if (isLogin()) {
                    openActivity(MeActivity.class);
                }
                break;
            case R.id.rl_zichan:
                if (isLogin()) {
                    openActivity(TotalMoneyActivity.class);
                }
                break;
            case R.id.rl_trade_record:
                if (isLogin()) {
                    openActivity(TradeRecordActivity.class);
                }
                break;
            case R.id.rl_bank:
                if (isLogin()) {
                    if (isBindingBankCard) {
                        openActivity(BankCardActivity.class);
                    } else {
                        openActivity(BindingBankCardActivity.class);
                    }
                }
                break;
            case R.id.rl_act:
                openActivity(HuoDongActivity.class);
                break;
            case R.id.rl_msg:
                openActivity(MsgCenterActivity.class);
                break;
            case R.id.rl_help_center:
                openActivity(HelpCenterActivity.class);
                break;
            case R.id.rl_invite_friend:
                openActivity(InviteFriendActivity.class);
                break;
            case R.id.rl_setting:
                if (isLogin()) {
                    openActivity(SettingActivity.class);
                }
                break;
            default:
                break;
        }
    }


    @Override
    public void onSuccess(Object object) {
        MineFramentInfoBean bean = (MineFramentInfoBean) object;
        if (bean.getError() == -1) {
            //用户名
            tvMinefragmentLogin.setText(bean.getUserName());
            isBindingBankCard = bean.isBindCardFlag();
            imgPath = bean.getHeadImg();
            if (!TextUtils.isEmpty(imgPath)) {
                String path = RetrofitHandle.BASE+imgPath;
                Glide.with(this).load(path).transform(new CenterCrop(getContext()), new GlideCircleTransform(getContext()))
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .crossFade().into(ivHeadPic);
                SpConfig.getInstance().edit().putString(Constants.IMAGE_PATH, path).commit();
                SpConfig.getInstance().edit().putString(Constants.ID, bean.getId()).commit();
                SpConfig.getInstance().edit().putString(Constants.TOKEN, bean.getToken()).commit();
                SpConfig.getInstance().edit().putBoolean(Constants.BIND_CARD_FLAG, bean.isBindCardFlag()).commit();
            } else {
                ivHeadPic.setImageResource(R.mipmap.icon_personal);
            }
            //银行卡是否绑定
            if (!bean.isBindCardFlag()) {
                tvMineBank.setText("未绑定");
            }
        } else {
            Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 101;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mHidden = hidden;
        if (!hidden) {
            doNetWork();
        }
    }

    private void doNetWork() {
        if (SpConfig.getInstance().getBool(Constants.IS_LOGIN)) {
            mineFramentInfoPresent = new MineFramentInfoPresent(getContext(), this);
            mineFramentInfoPresent.getMineFramentInfo();
        }
    }
}
